package com.yunban.project.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.template.mapper.PersonalizedConfigMapper;
import com.yunban.project.template.model.domain.PersonalizedConfig;
import com.yunban.project.template.model.dto.PersonalizedConfigReq;
import com.yunban.project.template.model.vo.PersonalizedConfigResp;
import com.yunban.project.template.service.PersonalizedConfigService;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: lijiajun
 * @date: 2025-02-13
 * @version: 1.0
 */
@Service
public class PersonalizedConfigServiceImpl extends ServiceImpl<PersonalizedConfigMapper, PersonalizedConfig> implements PersonalizedConfigService {
    @Override
    public PersonalizedConfig findById(long id) {
        PersonalizedConfig personalizedConfig = this.getById(id);
        if (personalizedConfig == null) {
            throw new BusinessException("信息不存在~");
        }
        return personalizedConfig;
    }

    @Override
    public List<PersonalizedConfigResp> getPersonalizedConfigInfos() {
        // 获取当前登录人信息
        Long userId = SmartRequestUtil.getRequestUserId();
        // 根据当前用户id查询个人化配置信息
        LambdaQueryWrapper<PersonalizedConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PersonalizedConfig::getCreatedBy, userId);
        List<PersonalizedConfig> list = this.list(wrapper);
        return SmartPageUtil.convert(list, PersonalizedConfigResp.class);
    }

    @Override
    public Boolean savePersonalizedConfig(PersonalizedConfigReq req) {
        // 首先需要注意几个点，一个用户是否只会有一个个性化的配置，暂时先做成用户可以有多套
        PersonalizedConfig personalizedConfig = new PersonalizedConfig();
        BeanUtils.copyProperties(req, personalizedConfig);
        return this.save(personalizedConfig);
    }

    @Override
    public Boolean editPersonalizedConfig(long id, PersonalizedConfigReq req) {
        PersonalizedConfig personalizedConfig = this.findById(id);
        BeanUtils.copyProperties(req, personalizedConfig);
        return this.updateById(personalizedConfig);
    }

    @Override
    public Boolean removePersonalizedConfig(long id) {
        PersonalizedConfig personalizedConfig = this.findById(id);
        personalizedConfig.setDeleted(true);
        return this.updateById(personalizedConfig);
    }
}